//
//  MessageMyOderViewModel.m
//  GLGW_Shoping
//
//  Created by yang on 2018/11/20.
//  Copyright © 2018年 yang. All rights reserved.
//

#import "MessageMyOderViewModel.h"
#import "MessageMyOrderTableViewCell.h"
#import "MessageGoodsTableViewCell.h"
#import "MessageIListModel.h"

@interface MessageMyOderViewModel ()<UITableViewDelegate,UITableViewDataSource>
{
    UITableView * selfTableview;
    //
    SystemMessagePType messType;
    MessageIListModel * messageModel;
    
}
@end

@implementation MessageMyOderViewModel

-(instancetype)initWithViewToViewModel:(UITableView *)tableView theCellType:(SystemMessagePType)p_type theSuperView:(UIView *)superView{
    if (self = [super initWithViewToViewModel:tableView theSuperView:superView]) {
        messType = p_type;
        tableView.mj_footer.hidden = YES;
        tableView.delegate = self;
        tableView.dataSource = self;
        UIView * headerView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, KScreenWidth, kHeightProportion_In_iPhoneTriploid(10))];
        headerView.backgroundColor = [UIColor colorWithHexString:@"#F2F2F2"];
        tableView.tableHeaderView = headerView;
        [superView addSubview:tableView];
        selfTableview= tableView;
        if (messType == SystemMessagePType_MyOrder) {
            [self requestSystemData];
        }else if (messType == SystemMessagePType_Goods){
            [self requestSystemData];
        }
        
    }
    return self;
}

#pragma mark -- 数据请求
-(void)requestSystemData{
    NSDictionary * dic = @{@"userId":koString(self.userId),@"type":@(messType)};
    [GLGWRequestTool POST:[NSString stringWithFormat:@"%@%@",kApiPrefix,kGetSysteMessages] lodingView:nil lodingTextString:@"" isLoding:YES parameters:dic success:^(id  _Nullable response) {
        self->messageModel = [MessageIListModel yy_modelWithJSON:response];
        if (self->messageModel.code == 0) {
            [self->selfTableview reloadData];
        }
    } failure:^(NSError * _Nullable error) {
        
    }];
}

#pragma mark --- UITableViewDelegate,UITableViewDataSource
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return 6;
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    CGFloat cellHeight;
    switch (messType) {
        case SystemMessagePType_MyOrder:{
            cellHeight = kHeightProportion_In_iPhoneTriploid(160);
        }
            break;
        case SystemMessagePType_Goods:{
            cellHeight = kHeightProportion_In_iPhoneTriploid(138);
        }
            break;
        case SystemMessagePType_Nitf:{
            cellHeight = kHeightProportion_In_iPhoneTriploid(135);
        }
            break;
            
        default:
            cellHeight = 0.01;
            break;
    }
    return cellHeight;
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    BaseTableViewCell * cell;
    switch (messType) {
        case SystemMessagePType_MyOrder:{
            MessageMyOrderTableViewCell * cell_Oreder = [MessageMyOrderTableViewCell cellWithTableView:tableView];
            cell = cell_Oreder;
        }
            break;
        case SystemMessagePType_Goods:
        case SystemMessagePType_Nitf:{
            MessageGoodsTableViewCell * cell_goods = [MessageGoodsTableViewCell cellWithTableView:tableView];
            cell_goods.sy_type = messType;
            cell = cell_goods;
        }
            break;
        default:
            break;
    }
    
    return cell;
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    if (self.delegate && [self.delegate respondsToSelector:@selector(didSelectRowAtIndexPathMessage:)]) {
        [self.delegate didSelectRowAtIndexPathMessage:indexPath];
    }
}

@end
